import Konva from "konva";
import { Draw } from "../../../Draw/Draw";
import { IKonva } from "../../../Interface/IKonva";

export function lineControlDragEnd(e: IKonva.Event, draw: Draw) {
  const layer = draw.getLayer();
  if (!layer) return;

  // 获取 target
  const control = e.currentTarget;
  // 获取当前线段
  const lineID = control.attrs.pid;
  const line = <Konva.Line>layer.findOne("#" + lineID);

  const stage = draw.getStage();
  // 清空 stage 数据
  stage.setAttrs({ positionCache: null });

  // 协同广播更新后的 points
  draw.broadcast({
    type: `${line.id()}_updatePoints`,
    payload: {
      layerID: layer.id(),
      shapeID: line.id(),
      points: line.points(),
    },
  });

  draw.render();
}
